package com.dnjn.happiness.survey.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.config.DateTimeFormatHolder;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.DateUtils;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.Check;
import com.dnjn.happiness.survey.domain.CheckItem;
import com.dnjn.happiness.survey.domain.CheckQuestion;
import com.dnjn.happiness.survey.domain.vo.CheckItemVO;
import com.dnjn.happiness.survey.domain.vo.CheckVO;
import com.dnjn.happiness.survey.mapper.CheckMapper;
import com.dnjn.happiness.survey.service.ICheckItemService;
import com.dnjn.happiness.survey.service.ICheckQuestionService;
import com.dnjn.happiness.survey.service.ICheckService;
import com.dnjn.happiness.survey.domain.Tiku;
import com.dnjn.happiness.survey.domain.TikuTags;
import com.dnjn.happiness.survey.service.ITikuService;
import com.dnjn.happiness.survey.service.ITikuTagsService;
import com.dnjn.happiness.system.api.RemoteTeamService;
import com.dnjn.happiness.system.api.domain.SysTeam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 考核记录Service业务层处理
 *
 * @author happiness
 * @date 2024-04-01
 */
@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements ICheckService {

    @Autowired
    private RemoteTeamService sysTeamService;

    @Autowired
    private ICheckItemService checkItemService;

    @Autowired
    private ICheckQuestionService questionService;

    @Autowired
    private ITikuTagsService tikuTagsService;

    @Autowired
    private ITikuService tikuService;


    @Override
    public List<SysTeam> getCheckList(CheckVO vo) {
        Long loginDeptId = SecurityUtils.getLoginUser().getDeptId();
        vo.setLoginDeptId(loginDeptId);

        List<SysTeam> sysTeams = baseMapper.getCheckList(vo);


        //   过滤普通员工
        List<SysTeam> employees = new ArrayList<>();
        R<List<SysTeam>> result = sysTeamService.getEmployees(sysTeams, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            employees = result.getData();
        }
        return employees;
    }

    @Override
    public List<SysTeam> getCheck(CheckVO vo) {
        Long loginDeptId = SecurityUtils.getLoginUser().getDeptId();
        vo.setLoginDeptId(loginDeptId);
        //递归所有 查询有下级的列表
        List<SysTeam> listAll = baseMapper.getCheckList(vo);
        R<List<SysTeam>> result = sysTeamService.getLeaderList(listAll, SecurityConstants.INNER);
        List<SysTeam> returnList = new ArrayList<>();
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            returnList = result.getData();
        }
        return returnList;

    }

    @Override
    public List<SysTeam> getByManagerId(Long userId) {
        R<List<SysTeam>> result = sysTeamService.getManager(userId, SecurityConstants.INNER);
        List<SysTeam> returnList = new ArrayList<>();
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            returnList = result.getData();
        }
        return returnList;

    }

    @Override
    public List<SysTeam> getSubordinate(Long userId) {
        R<List<SysTeam>> result = sysTeamService.getSubordinate(userId, SecurityConstants.INNER);
        List<SysTeam> returnList = new ArrayList<>();
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
            returnList = result.getData();
        }
        return returnList;
    }

    @Override
    public int addCheck(Check check) {
        List<CheckItem> checkItems = check.getCheckItemList();
        List<Long> idList = check.getUserIdList();
        if (check.getCheckType().equals("STAFF")) {
            checkItems = new ArrayList<>();
            for (Long userId : idList) {
                CheckItem checkItem = new CheckItem();

                checkItem.setUserId(userId);
                checkItems.add(checkItem);
            }
        }

        check.setCheckTotal(checkItems.size());

        int insert = baseMapper.insert(check);
        Long loginId = SecurityUtils.getUserId();


        checkItems.forEach(checkItem -> {
            if(check.getCheckType().equals("STAFF")){
                R<SysTeam> result = sysTeamService.getMangerUpOne(checkItem.getUserId(), SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
                    checkItem.setSuperiorUserId(result.getData().getUserId());
                }
            }
            checkItem.setInitiatorId(loginId).setCheckId(check.getCheckId()).setCheckType(check.getCheckType());
            checkItemService.save(checkItem);

            updateTeamCheckTime(checkItem.getUserId());//todo
        });
        queryTiku(check);
        return insert;


    }

    private void updateTeamCheckTime(Long userId) {
        sysTeamService.updateTeamCheckTimeByUserId(userId, SecurityConstants.INNER);
    }

    private void queryTiku(Check check) {


        // 从题库tag查是员工还是mananger,
        String checkType = check.getCheckType();
        if (checkType.equals(Constants.TIKU_CATEGORY_STAFF)) {
            TikuTags tikuTag = tikuTagsService.lambdaQuery().eq(TikuTags::getDeleted, Constants.UNDELETED).eq(TikuTags::getTikuType, Constants.TIKU_TYPE_CHECK).eq(TikuTags::getTikuCategory, Constants.TIKU_CATEGORY_STAFF).one();
            if (null != tikuTag) {
                List<Tiku> tikuList = tikuService.lambdaQuery().eq(Tiku::getDeleted, Constants.UNDELETED).eq(Tiku::getTikuTagsId, tikuTag.getTikuTagsId()).list();
                //copy
                tikuList.stream().forEach(t -> {
                    CheckQuestion question = new CheckQuestion();
                    question.setIsSelectPack(t.getIsSelectPack());
                    question.setScoreOptionA(t.getScoreOptionA());
                    question.setScoreOptionB(t.getScoreOptionB());
                    question.setScoreOptionC(t.getScoreOptionC());
                    question.setScoreOptionD(t.getScoreOptionD());
                    question.setScoreOptionE(t.getScoreOptionE());
                    question.setScoreOptionF(t.getScoreOptionF());
                    question.setScoreOptionG(t.getScoreOptionG());
                    question.setOptionA(t.getOptionA());
                    question.setOptionB(t.getOptionB());
                    question.setOptionC(t.getOptionC());
                    question.setOptionD(t.getOptionD());
                    question.setOptionE(t.getOptionE());
                    question.setOptionF(t.getOptionF());
                    question.setOptionG(t.getOptionG());
                    question.setIsMultipleChoice(t.getIsMultipleChoice());
                    question.setAnswer(t.getAnswer());
                    question.setCheckId(check.getCheckId());
                    question.setTitle(t.getTitle());
                    question.setTikuId(t.getTikuId());
                    questionService.save(question);
                });
            }

        }
        if (checkType.equals(Constants.TIKU_CATEGORY_MANAGER)) {
            TikuTags tikuTag = tikuTagsService.lambdaQuery().eq(TikuTags::getDeleted, Constants.UNDELETED).eq(TikuTags::getTikuType, Constants.TIKU_TYPE_CHECK).eq(TikuTags::getTikuCategory, Constants.TIKU_CATEGORY_MANAGER).one();
            if (null != tikuTag) {
                List<Tiku> tikuList = tikuService.lambdaQuery().eq(Tiku::getDeleted, Constants.UNDELETED).eq(Tiku::getTikuTagsId, tikuTag.getTikuTagsId()).list();

                //copy
                tikuList.stream().forEach(t -> {
                    CheckQuestion question = new CheckQuestion();
                    question.setIsSelectPack(t.getIsSelectPack());
                    question.setScoreOptionA(t.getScoreOptionA());
                    question.setScoreOptionB(t.getScoreOptionB());
                    question.setScoreOptionC(t.getScoreOptionC());
                    question.setScoreOptionD(t.getScoreOptionD());
                    question.setScoreOptionE(t.getScoreOptionE());
                    question.setScoreOptionF(t.getScoreOptionF());
                    question.setScoreOptionG(t.getScoreOptionG());
                    question.setOptionA(t.getOptionA());
                    question.setOptionB(t.getOptionB());
                    question.setOptionC(t.getOptionC());
                    question.setOptionD(t.getOptionD());
                    question.setOptionE(t.getOptionE());
                    question.setOptionF(t.getOptionF());
                    question.setOptionG(t.getOptionG());
                    question.setIsMultipleChoice(t.getIsMultipleChoice());
                    question.setAnswer(t.getAnswer());
                    question.setCheckId(check.getCheckId());
                    question.setTitle(t.getTitle());
                    question.setTikuId(t.getTikuId());
                    questionService.save(question);
                });
            }
        }
    }


    @Override
    public Page<Check> queryList(Page page, String checkType, Long deptId) {
        return baseMapper.queryList(page, checkType, deptId);
    }

    @Override
    public Page<CheckItemVO> queryCheckItemByCheckId(Page page, String nickName, String checkStatus, String checkId, Long deptId, Long teamId) {
        return checkItemService.queryCheckItemByCheckId(page, nickName, checkStatus, checkId, deptId, teamId);
    }

    @Override
    public List<CheckItem> getCheckItem(String checkStatus) {
        return checkItemService.getCheckItem(checkStatus);
    }



    @Override
    public List<CheckItemVO> queryCheckItemByCheckIdAndUserId(Long checkId, Long userId) {
        return checkItemService.queryCheckItemByCheckIdAndUserId(checkId, userId);
    }
}
